package com.kclm.xsap.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kclm.xsap.consts.OperateType;
import com.kclm.xsap.dao.MemberBindRecordDao;
import com.kclm.xsap.dao.MemberLogDao;
import com.kclm.xsap.entity.MemberBindRecordEntity;
import com.kclm.xsap.entity.MemberLogEntity;
import com.kclm.xsap.service.MemberBindRecordService;
import com.kclm.xsap.utils.exception.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Service
public class MemberBindRecordServiceImpl extends ServiceImpl<MemberBindRecordDao, MemberBindRecordEntity> implements MemberBindRecordService {
   @Autowired
   private MemberBindRecordDao memberBindRecordDao;
   @Autowired
   private MemberLogDao memberLogDao;
    @Override
    public String[] getCardName(Long id) {
        return baseMapper.selectCardName(id);
    }

    @Override
    public List<MemberBindRecordEntity> selectList(QueryWrapper<MemberBindRecordEntity> wrapper) {
        return baseMapper.selectList(wrapper);
    }

    @Override
    public String getName(Long cardId) {
        return baseMapper.selectName(cardId);
    }

    @Override
    public String getType(Long cardId) {
        return baseMapper.selectType(cardId);

    }

    @Override
    public boolean isUpdate(Long memberId, Long bindCardId, Integer status, String operator) {
        MemberBindRecordEntity memberBindRecordEntity = memberBindRecordDao.selectById(bindCardId);
        memberBindRecordEntity.setActiveStatus(status);
        try {
            memberBindRecordDao.updateById(memberBindRecordEntity);
        } catch (Exception e) {
            throw new RRException("更新卡状态失败！");
        }
        MemberLogEntity memberLogEntity=new MemberLogEntity();
        if (status==0){
            memberLogEntity.setType(OperateType.DEACTIVATE_MEMBERSHIP_CARD_OPERATION.getMsg());
        }else {
            memberLogEntity.setType(OperateType.ACTIVATE_MEMBERSHIP_CARD_OPERATION.getMsg());
        }
        memberLogEntity.setOperator(operator)
                .setInvolveMoney(BigDecimal.ZERO)
                .setMemberBindId(bindCardId)
                .setCardActiveStatus(status);
        try {
            memberLogDao.insert(memberLogEntity);
        } catch (Exception e) {
            throw new RRException("添加日志记录失败！");
        }
        return true;
    }
}
